/**
 * 获取按文件分类缺陷数据
 *
 * Created by yangyunze on 2021/04/06.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/static-analyze/gjb.json');

module.exports = describe('/GET /files/:fileId/defects/rules.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  let newUser;

  function url(fileId) {
    return `/files/${fileId}/defects/rules.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUser = tester;
        return newUser.createRandomProject('c++');
      })
      .then((res) => {
        project = res;
        return project.createStaticAnalyzeVersion(
          benchmark.verified_rules,
          benchmark.files,
          newUser
        );
      })
      .then((res) => {
        projectVersion = res;
        return projectVersion.rootDirectory.staticAnalyze();
      })
      .then(() => projectVersion.project.creator.waiting4TasksOfVersionFinished(
        projectVersion.versionId
      ))
      .then(done)
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers([newUser.userId])
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 获取按文件分类缺陷数据
  Object.keys(benchmark.result.fileDefects).forEach((fileName) => {
    it(`it should get defects group by file ${fileName} successfully`, function (done) {
      projectVersion.rootDirectory.getTree()
        .then((fileTree) => {
          let fileId;
          fileTree.tree.children.forEach((child) => {
            if (child.fileName !== fileName) {
              return;
            }
            fileId = child.id;
          });
          return fileId;
        })
        .then((fileId) => {
          chai.request(uri)
            .get(url(fileId))
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.equal.fileDefects(res.body, benchmark.result.fileDefects[fileName]);
              done();
            });
        })
        .catch(done);
    });
  });

  // 根据缺陷级别过滤按文件分类缺陷数据
  Object.keys(benchmark.result.fileTreeDefectLevelFilter).forEach((level) => {
    it(`it should get file tree filter by defect level ${level} successfully`, function (done) {
      chai.request(uri)
        .get(`/files/${projectVersion.rootDirectory.fileId}/tree.json`)
        .query({ depth: 1, level })
        .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(200);
          common.should.be.equal.fileTree(
            res.body,
            benchmark.result.fileTreeDefectLevelFilter[level]
          );
          done();
        });
    });
  });
});
